package com.whiteape.book.ui.base;

import android.app.ActivityOptions;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.gyf.immersionbar.ImmersionBar;
import com.gyf.immersionbar.components.ImmersionFragment;
import com.hwangjr.rxbus.RxBus;
import com.whiteape.book.R;

import java.util.Objects;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

import static com.whiteape.book.ui.base.BaseActivity.START_SHEAR_ELE;

/**
 * Created by newbiechen on 17-3-31.
 */

public abstract class BaseFragment extends ImmersionFragment {

    protected CompositeDisposable mDisposable;

    private View root = null;
    private Unbinder unbinder;

    @Override
    public void initImmersionBar() {
//        ImmersionBar.with(this)
//                .fitsSystemWindows(true)  //使用该属性,必须指定状态栏颜色
//                .statusBarDarkFont(true)
//                .statusBarColor(R.color.colorPrimary)
//                .init();

    }

//    @Override
//    public void onResume() {
//        super.onResume();
//    }

    @LayoutRes
    protected abstract int getContentId();

    /*******************************init area*********************************/
    protected void addDisposable(Disposable d) {
        if (mDisposable == null) {
            mDisposable = new CompositeDisposable();
        }
        mDisposable.add(d);
    }


    protected void initData(Bundle savedInstanceState) {
    }

    /**
     * 初始化点击事件
     */
    protected void initEvent() {
    }

    /**
     * 逻辑使用区
     */
    protected void startLogic() {
    }

    /**
     * 初始化零件
     */
    protected void initView(Bundle savedInstanceState) {
    }

    /******************************lifecycle area*****************************************/
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        int resId = getContentId();
        root = inflater.inflate(resId, container, false);
        return root;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        RxBus.get().register(this);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData(savedInstanceState);
        unbinder = ButterKnife.bind(this, root);
        initView(savedInstanceState);
        initImmersionBar();
        initEvent();
        startLogic();
    }

    protected void startActivityByAnim(Intent intent, @NonNull View view, @NonNull String transitionName, int animIn, int animExit) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            intent.putExtra(START_SHEAR_ELE, true);
            ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(getActivity(), view, transitionName);
            startActivity(intent, options.toBundle());
        } else {
            startActivityByAnim(intent, animIn, animExit);
        }
    }

    protected void startActivityByAnim(Intent intent, int animIn, int animExit) {
        startActivity(intent);
        Objects.requireNonNull(getActivity()).overridePendingTransition(animIn, animExit);
    }

    @Override
    public void onDetach() {
        super.onDetach();

        if (unbinder != null) {
            unbinder.unbind();
        }

        RxBus.get().unregister(this);

        if (mDisposable != null) {
            mDisposable.clear();
        }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }

    /**************************公共类*******************************************/
    public String getName() {
        return getClass().getName();
    }

    protected <VT> VT getViewById(int id) {
        if (root == null) {
            return null;
        }
        return (VT) root.findViewById(id);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        initImmersionBar();
    }
}


